//
//  CCYStatusPictureView.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/30.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit

// 图片间距
let CCYStatusPicturesViewItemMargin: CGFloat = 4
//图片宽高
let CCYStatusPictureViewItemWH: CGFloat = (UIScreen.width - 2 * CCYStatusPicturesViewItemMargin - 2 * CCYStatusCellMargin) / 3 

class CCYStatusPictureView: UIView {
    @IBOutlet var widthCons: NSLayoutConstraint!
    @IBOutlet var heightCons: NSLayoutConstraint!
    /// 一个装9个图片的数组
    private var imageViews = [UIImageView]()
    
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFont(ofSize: 20)
        label.textColor = UIColor.black
        label.text = "我还没有图片"
        label.frame = CGRect(x: 20, y: 20, width: 200, height: 100)
        
        return label
    }()
    
    var imageURLs: [URL]? {
        didSet{
            let count = imageURLs?.count ?? 0
            
            debugLabel.text = "图片数量 = \(count)"
            layoutImages(count: count)
            
            let size = calcSize(count: count)
            widthCons.constant = size.width
            heightCons.constant = size.height
        }
    }
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
        self.addSubview(debugLabel)
    }
    
    /// 配件视图添加9张图片
    private func setupUI() {
        for _ in 0..<9 {
            let image = UIImageView()
            
//            image.backgroundColor = UIColor.brown
            
            self.addSubview(image)
            imageViews.append(image)
        }
//        layoutImages(count: imageURLs?.count ?? 0)
    }
    
    /// 布局位置
    private func layoutImages(count: Int) {
        /// 默认列数 3
        var numberOfColum = 3
        
        if count == 1 {
            numberOfColum = 1
        }else if count == 2 || count == 4{
            numberOfColum = 2
        }
        
        for (index, imageView) in imageViews.enumerated() {
            if index >= count {
                imageView.isHidden = true
            }else{
                imageView.isHidden = false
                // 计算在哪一列
                let column = index % numberOfColum
                
                // 计算在哪行
                let row = index / numberOfColum
                
                
                
                let x = CGFloat(column) * (CCYStatusPicturesViewItemMargin + CCYStatusPictureViewItemWH)
                let y = CCYStatusCellMargin + CGFloat(row) * (CCYStatusPicturesViewItemMargin + CCYStatusPictureViewItemWH)
                
                
                let frame = CGRect(x: x, y: y, width: CCYStatusPictureViewItemWH, height: CCYStatusPictureViewItemWH)
                
                imageView.frame = frame
             
                if let url = imageURLs?[index]{
                    imageView.sd_setImage(with: url)
                }
            }
        }
    }
    
    
    ///计算配图大小
    private func calcSize(count: Int) -> CGSize{
        if count == 0 {
            return CGSize()
        }
        
        var numberOfColumn = 3
        
        if count == 1 {
            numberOfColumn = 1
        }else if count == 2 || count == 4 {
            numberOfColumn = 2
        }
        
        let numberOfRow = (count + numberOfColumn - 1) / numberOfColumn
        
        // 计算总宽 = 列数 * 图片的宽度 + (列数 - 1) * 间距
        let width = CGFloat(numberOfColumn) * CCYStatusPictureViewItemWH + (CGFloat(numberOfColumn - 1) * CCYStatusPicturesViewItemMargin)
        
        // 计算高度 = 行数 * 图片高度 + (行数 - 1) * 间距
        let height = CGFloat(numberOfRow) * CCYStatusPictureViewItemWH + (CGFloat(numberOfRow - 1) * CCYStatusPicturesViewItemMargin)
        
        return CGSize(width: width, height: height)
    }
    
    
    
}
